package com.bytz.config.security;

import cn.hutool.core.collection.CollectionUtil;
import com.bytz.modules.cms.reseller.constant.ResellerConstant;
import com.bytz.modules.cms.reseller.service.IResellerService;
import com.bytz.modules.system.security.vo.LoginReseller;
import com.bytz.modules.system.service.impl.UserDetailsServiceBaseImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Set;

@Service
public class MallUserServiceDetailImpl extends UserDetailsServiceBaseImpl {

    @Autowired
    private IResellerService resellerService;

    @Override
    protected String getCachePrefix() {
        return ResellerConstant.ResellerSecurity.SECURITY_CACHE_PREFIX;
    }

    @Override
    protected UserDetails getUserBasicInfo(String username) {
        return resellerService.getForLogin(username);
    }

    @Override
    protected boolean validateUser(UserDetails user, String username) {
        return this.resellerService.checkReseller((LoginReseller)user, username, true);
    }

    @Override
    protected Set<String> getUserRoles(UserDetails userDetails) {
        return CollectionUtil.set(false, ResellerConstant.ResellerSecurity.RESELLER_ROLE_CODE);
    }

    @Override
    protected Set<String> getUserPermissions(UserDetails userDetails) {
        // 供应商不用判断权限
        return Collections.EMPTY_SET;
    }

    @Override
    protected void setAuthorities(UserDetails userDetails, Set<String> authorities) {
        ((LoginReseller)userDetails).setPermissions(authorities);
    }
}
